package com.sctu.EMSApp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.*;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import com.sctu.adapter.GuideAdapter;

import java.util.ArrayList;

/**
 * Created by fll on 2015/12/09.
 */

public class GuideActivity extends Activity implements View.OnTouchListener {

    private ViewPager viewPager;
    private ArrayList<View> pageViews;
    private ImageView imageView;
    private ImageView[] imageViews;
    // 包裹滑动图片LinearLayout
    private ViewGroup main;
    // 包裹小圆点的LinearLayout
    private LinearLayout group;
    private SharedPreferences.Editor editor;
    private Boolean isFirstIn;
    private int lastX = 0;
    private int currentIndex = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences preferences = getSharedPreferences("FIRST", MODE_PRIVATE);
        isFirstIn = preferences.getBoolean("isFirstIn", true);

        if (isFirstIn) {
            editor = preferences.edit();
            editor.putBoolean("isFirstIn", false);
            editor.commit();

        } else {
            Intent intent = new Intent(GuideActivity.this, MyActivity.class);
            startActivity(intent);
            finish();
        }
        // 设置无标题窗口
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        int[] img = new int[]{R.drawable.guide_page_first, R.drawable.guide_page_first, R.drawable.guide_page_first};

        LayoutInflater inflater = getLayoutInflater();
        pageViews = new ArrayList<View>();
        for (int i = 0; i < img.length; i++) {
            LinearLayout layout = new LinearLayout(this);
            LinearLayout.LayoutParams ltp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            final ImageView imageView = new ImageView(this);
            imageView.setScaleType(ScaleType.FIT_XY);
            imageView.setImageResource(img[i]);
            imageView.setPadding(0, 0, 0, 0);
            layout.addView(imageView, ltp);
            pageViews.add(layout);
        }
        imageViews = new ImageView[pageViews.size()];
        main = (ViewGroup) inflater.inflate(R.layout.guide_view, null);
        group = (LinearLayout) main.findViewById(R.id.viewGroup);
        viewPager = (ViewPager) main.findViewById(R.id.guidePages);

        /**
         * 有几张图片 下面就显示几个小圆点
         */

        for (int i = 0; i < pageViews.size(); i++) {
            LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            //设置每个小圆点距离左边的间距
            margin.setMargins(10, 0, 0, 0);
            imageView = new ImageView(GuideActivity.this);
            //设置每个小圆点的宽高
            imageView.setLayoutParams(new LinearLayout.LayoutParams(12,12));
            imageViews[i] = imageView;
            if (i == 0) {
                // 默认选中第一张图片
                imageViews[i]
                        .setBackgroundResource(R.drawable.guide_dot_click);
            } else {
                //其他图片都设置未选中状态
                imageViews[i]
                        .setBackgroundResource(R.drawable.guide_dots);
            }
            group.addView(imageViews[i], margin);
        }

        setContentView(main);
        //给viewpager设置适配器
        viewPager.setAdapter(new GuideAdapter(pageViews));
        //给viewpager设置监听事件
        viewPager.setOnPageChangeListener(new MypageChangeListener());
        viewPager.setOnTouchListener(this);
    }

    private class MypageChangeListener implements OnPageChangeListener {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int arg0) {

            currentIndex = arg0;
            //遍历数组让当前选中图片下的小圆点设置颜色
            for (int i = 0; i < imageViews.length; i++) {
                imageViews[arg0]
                        .setBackgroundResource(R.drawable.guide_dot_click);

                if (arg0 != i) {
                    imageViews[i]
                            .setBackgroundResource(R.drawable.guide_dots);
                }
            }
            Button btn = (Button) main.findViewById(R.id.start);

            if (arg0 == pageViews.size() - 1) {

                btn.setVisibility(View.VISIBLE);
                btn.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        GoToMainActivity();
                    }
                });
            }
            if (arg0 != pageViews.size() - 1) {
                btn.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    }

    public void GoToMainActivity() {
        Intent i = new Intent(GuideActivity.this, MyActivity.class);
        startActivity(i);
        finish();

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                if ((lastX - event.getX()) > 100 && (currentIndex == pageViews.size() - 1)) {
                    GoToMainActivity();
                }
                break;
            default:
                break;
        }
        return false;
    }
}
